import asyncio
import random
import types

all_potato = []


class Potato:

    def __init__(self, code=0):
        self.code = code

    def make(self, num):
        print('making...')
        potato_list = []
        for i in range(num):
            potato_list.append(Potato(i + 1))
        return potato_list

    def __repr__(self):
        return f'Potato--{self.code}'


async def ask_for_potato():
    await asyncio.sleep(random.random())
    all_potato.extend(Potato().make(random.randint(1, 10)))


async def take_potato(num):
    count = 0
    while count <= num:
        if len(all_potato) == 0:
            await ask_for_potato()

        potato = all_potato.pop()
        yield potato
        count += 1


async def buy_potato():
    bucket = []
    async for p in take_potato(50):
        bucket.append(p)
        print(f'Got potato {p}...')


asyncio.run(buy_potato())
